package command;

import model.impl.Passenger;
import model.impl.PassengerImpl;
import DTO.DTOManager;
import DTO.PassengerDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/**
 * Class represents command interface;
 * It checks that the received login password and user info from client user 
 * are correct and not busy in case that it's true sends OK and the DTO object 
 * that represents new registered passenger, if it's not that it sends NO 
 * and null.
 */
public class ClientRegistration implements Command {

	private Protocol protocol = new Protocol();
	
	@Override
	public void execute(ReceivedData recived) {
		model.Passenger passengerImpl = new PassengerImpl();
		Protocol.Message message = this.protocol.decodeMessage(recived.data);
		PassengerDTO passengerDTO = (PassengerDTO)message.getObject();
		Passenger passenger = passengerImpl.registrationOfNewPassenger(passengerDTO);
		if(passenger != null) {
			passengerDTO = (new DTOManager()).createPassengerDTO(passenger);
			Protocol.Message answer = this.protocol.new Message("ok", passengerDTO);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		} else {
			Protocol.Message answer = this.protocol.new Message("no", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		}	
	}
}
